<template>
  <div class="payment-page">
    <!-- 头部 -->
    <div class="header">
      <div class="back" @click="$router.back()">
        <i class="iconfont icon-back"></i>
      </div>
      <div class="title">在线支付</div>
    </div>

    <!-- 订单信息卡片 -->
    <div class="order-card">
      <div class="shop-info">
        <img :src="orderInfo?.business?.businessImg" :alt="orderInfo?.business?.businessName">
        <div class="shop-detail">
          <div class="shop-name">{{ orderInfo?.business?.businessName }}</div>
          <div class="order-number">订单号：{{ orderInfo?.orderId }}</div>
        </div>
      </div>
      <div class="price-info">
        <div class="label">支付金额</div>
        <div class="price">
          <small>￥</small>
          <span>{{ orderInfo?.orderTotal }}</span>
        </div>
      </div>
    </div>

    <!-- 支付方式卡片 -->
    <div class="payment-card">
      <div class="card-title">
        <i class="iconfont icon-payment"></i>
        <span>选择支付方式</span>
      </div>
      <div class="payment-options">
        <div class="payment-option" 
             :class="{ active: payMethod === 'alipay' }"
             @click="payMethod = 'alipay'">
          <div class="option-left">
            <div class="icon-wrapper alipay">
              <img src="/img/alipay.png" alt="支付宝">
            </div>
            <div class="option-info">
              <div class="option-name">支付宝支付</div>
              <div class="option-desc">推荐支付宝用户使用</div>
            </div>
          </div>
          <div class="check-circle" :class="{ checked: payMethod === 'alipay' }"></div>
        </div>
        <div class="payment-option"
             :class="{ active: payMethod === 'wechat' }"
             @click="payMethod = 'wechat'">
          <div class="option-left">
            <div class="icon-wrapper wechat">
              <img src="/img/wechat.png" alt="微信支付">
            </div>
            <div class="option-info">
              <div class="option-name">微信支付</div>
              <div class="option-desc">推荐微信用户使用</div>
            </div>
          </div>
          <div class="check-circle" :class="{ checked: payMethod === 'wechat' }"></div>
        </div>
      </div>
    </div>

    <!-- 底部支付栏 -->
    <div class="bottom-bar">
      <div class="amount-info">
        <div class="amount-row">
          <span class="label">实付金额：</span>
          <span class="price">￥{{ orderInfo?.orderTotal }}</span>
        </div>
        <div class="time-limit">请在15:00内完成支付</div>
      </div>
      <button class="pay-btn" @click="confirmPayment">确认支付</button>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import axios from 'axios'

const router = useRouter()
const route = useRoute()
const orderInfo = ref(null)
const payMethod = ref('alipay')

// 获取订单信息
const getOrderInfo = async () => {
  try {
    const res = await axios.get('/orders/getOrdersById', {
      params: { orderId: route.query.orderId }
    })
    orderInfo.value = res.data
  } catch (error) {
    console.error('获取订单信息失败:', error)
  }
}

// 确认支付
const confirmPayment = async () => {
  try {
    await axios.post('/orders/updateOrderState', null, {
      params: {
        orderId: route.query.orderId,
        orderState: 1  // 1表示已支付
      }
    })
    alert('支付成功')
    router.push('/orderList')
  } catch (error) {
    console.error('支付失败:', error)
    alert('支付失败，请重试')
  }
}

onMounted(() => {
  getOrderInfo()
})
</script>

<style scoped>
.payment-page {
  min-height: 100vh;
  background: #f7f8fa;
  padding-top: 44px;
  padding-bottom: 70px;
}

.header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 44px;
  background: #0097ff;
  color: #fff;
  display: flex;
  align-items: center;
  padding: 0 15px;
  z-index: 100;
}

.back {
  padding: 10px;
  margin-left: -10px;
}

.title {
  flex: 1;
  text-align: center;
  font-size: 16px;
}

.order-card {
  margin: 15px;
  background: #fff;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.06);
}

.shop-info {
  padding: 15px;
  display: flex;
  align-items: center;
  background: linear-gradient(135deg, #0097ff 0%, #0066cc 100%);
  color: #fff;
}

.shop-info img {
  width: 50px;
  height: 50px;
  border-radius: 8px;
  margin-right: 12px;
  border: 2px solid rgba(255, 255, 255, 0.2);
}

.shop-detail {
  flex: 1;
}

.shop-name {
  font-size: 16px;
  font-weight: 500;
  margin-bottom: 4px;
}

.order-number {
  font-size: 12px;
  opacity: 0.8;
}

.price-info {
  padding: 20px;
  text-align: center;
}

.label {
  font-size: 14px;
  color: #666;
  margin-bottom: 8px;
}

.price {
  color: #ff6b01;
}

.price small {
  font-size: 16px;
  margin-right: 2px;
}

.price span {
  font-size: 28px;
  font-weight: 600;
}

.payment-card {
  margin: 15px;
  background: #fff;
  border-radius: 12px;
  padding: 20px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.06);
}

.card-title {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
  font-size: 16px;
  font-weight: 500;
}

.card-title i {
  margin-right: 8px;
  color: #0097ff;
}

.payment-option {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 15px;
  margin: 10px 0;
  border-radius: 8px;
  background: #f8f9fa;
  transition: all 0.3s ease;
}

.payment-option.active {
  background: #f0f7ff;
}

.option-left {
  display: flex;
  align-items: center;
}

.icon-wrapper {
  width: 40px;
  height: 40px;
  border-radius: 8px;
  margin-right: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #f5f5f5;
}

.icon-wrapper.alipay {
  background: linear-gradient(135deg, #00a0e9 0%, #0066cc 100%);
}

.icon-wrapper.wechat {
  background: linear-gradient(135deg, #51c332 0%, #2ba01c 100%);
}

.icon-wrapper img {
  width: 25px;
  height: 25px;
  filter: brightness(0) invert(1);
}

.option-info {
  flex: 1;
}

.option-name {
  font-size: 15px;
  font-weight: 500;
  margin-bottom: 4px;
}

.option-desc {
  font-size: 12px;
  color: #999;
}

.check-circle {
  width: 22px;
  height: 22px;
  border: 2px solid #ddd;
  border-radius: 50%;
  position: relative;
  transition: all 0.3s ease;
}

.check-circle.checked {
  border-color: #0097ff;
  background: #0097ff;
}

.check-circle.checked::after {
  content: '';
  position: absolute;
  width: 8px;
  height: 8px;
  background: #fff;
  border-radius: 50%;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.bottom-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: #fff;
  padding: 12px 15px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.05);
}

.amount-info {
  flex: 1;
}

.amount-row {
  display: flex;
  align-items: baseline;
}

.amount-row .label {
  font-size: 14px;
  color: #666;
  margin: 0;
}

.amount-row .price {
  font-size: 20px;
  font-weight: bold;
  margin-left: 5px;
}

.time-limit {
  font-size: 12px;
  color: #999;
  margin-top: 2px;
}

.pay-btn {
  margin-left: 15px;
  padding: 0 25px;
  height: 40px;
  background: linear-gradient(135deg, #0097ff 0%, #0066cc 100%);
  color: #fff;
  border: none;
  border-radius: 20px;
  font-size: 15px;
  font-weight: 500;
  box-shadow: 0 4px 12px rgba(0, 151, 255, 0.3);
  transition: all 0.3s ease;
}

.pay-btn:active {
  transform: scale(0.98);
  box-shadow: 0 2px 6px rgba(0, 151, 255, 0.2);
}
</style>